//
//  ViewController.m
//  团购菜单
//
//  Created by 伍银荣 on 2021/6/3.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "ViewController.h"
#import "Goods.h"
#import "GoodsCell.h"
#import "GoodsFooterView.h"
#import "GoodsHeaderView.h"

@interface ViewController ()<UITableViewDataSource,GoodsFooterViewDelegate>
@property (nonatomic, strong) NSMutableArray *goodsArr;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

-(NSMutableArray *)goodsArr {
    if (!_goodsArr) {
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"goods.plist" ofType:nil]];
        NSMutableArray *mArr = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            Goods *goods = [Goods goodsWithDictionary:dict];
            [mArr addObject:goods];
        }
        _goodsArr = mArr;
    }
    return _goodsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 60;
    
    GoodsFooterView *footerView = [GoodsFooterView goodsFooterView];
    footerView.delegate = self;
    self.tableView.tableFooterView = footerView;
    
    GoodsHeaderView *headerView = [GoodsHeaderView goodsHeaderView];
    self.tableView.tableHeaderView = headerView;
   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.goodsArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Goods *goods = self.goodsArr[indexPath.row];
    goodsCell *cell = [goodsCell goodsCellWithTableView:tableView];
    cell.goodsModel = goods;
    return cell;
}

-(void)updateDataWithGoodsFooterView:(GoodsFooterView *)goodsFooterView {
    //创建模型数据
    Goods *goods = [[Goods alloc]init];
    goods.title = @"WODE";
    goods.price = @"12.5";
    goods.icon = @"2";
    goods.buyCount = @"2000";
    //添加一条模型数据到数据源数组中
    [self.goodsArr addObject:goods];
    //刷新cell
    [self.tableView reloadData];
    //把tableView上最后一行的数据滚动到最上面
    NSIndexPath *idxPath = [NSIndexPath indexPathForRow:self.goodsArr.count - 1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:idxPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


@end
